# -*- coding: utf-8 -*-
# @Author    : Tian Wencheng
# @CreateTime: 2021/1/9 20:37
# @FileName  : common.py
# @IDE       : PyCharm
# @Git       : https://gitee.com/dianetian/projects


from common.request import HTTPRequest
from interface.base import BaseModule


class CommonModule(BaseModule):
    def __init__(self):
        self.request_url = ""
        self.request_method = ""
        self.request_headers = {"Content-Type": "application/json"}
        self.request_params = None
        self.cookies = super().cookies
        self.web_url = super().web_url

    def get_camera_tree(self, **kwargs):
        show_camera_flag = kwargs.get("showCamera")
        self.request_url = "/api/cameraTree"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"showCamera": show_camera_flag}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_camera_tree_info(self):
        self.request_url = "/api/areas"
        url = self.web_url + self.request_url
        self.request_method = "GET"

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_user_visible_menus(self):
        self.request_url = "/api/user/listMenu"
        url = self.web_url + self.request_url
        self.request_method = "GET"

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_camera_fault_types(self):
        self.request_url = "/api/manager/getVideoQualityType"
        url = self.web_url + self.request_url
        self.request_method = "GET"

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def kill_stream_server(self, **kwargs):
        ip = kwargs.get("ip")
        self.request_url = "/api/killJfss"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"ip": ip}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_event_detail(self, **kwargs):
        current_page = kwargs.get("currentPage")
        page_size = kwargs.get("pageSize")
        alarm_grade = kwargs.get("alarmGrade")
        start_time = kwargs.get("startTime")
        end_time = kwargs.get("endTime")
        event_type = kwargs.get("eventType")
        areas = kwargs.get("areas[]")
        process_states = kwargs.get("processingStates[]")
        remark = kwargs.get("remark")
        camera_ids = kwargs.get("cameraIds")

        self.request_url = "/api/event/detail"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"currentPage": current_page, "pageSize": page_size, "alarmGrade": alarm_grade,
                               "startTime": start_time, "endTime": end_time, "eventType": event_type, "areas[]": areas,
                               "processingStates[]": process_states, "remark": remark, "cameraIds": camera_ids}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_event_statistics_by_region(self, **kwargs):
        start_time = kwargs.get("startTime")
        end_time = kwargs.get("endTime")
        alarm_grade = kwargs.get("alarmGrade")
        interval = kwargs.get("interval")

        self.request_url = "/api/event/statistics/regionalEvent"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"interval": interval, "alarmGrade": alarm_grade, "startTime": start_time,
                               "endTime": end_time}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def get_statistics_by_event_type(self, **kwargs):
        start_time = kwargs.get("startTime")
        end_time = kwargs.get("endTime")
        alarm_grade = kwargs.get("alarmGrade")
        interval = kwargs.get("interval")
        unit_id = kwargs.get("unitId")

        self.request_url = "/api/event/statistics/eventType"
        url = self.web_url + self.request_url
        self.request_method = "GET"
        self.request_params = {"interval": interval, "alarmGrade": alarm_grade, "startTime": start_time,
                               "endTime": end_time, "unitId": unit_id}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def dispose_event(self, **kwargs):
        method = kwargs.get("method")
        event_id = kwargs.get("eventId")
        remark = kwargs.get("remark")

        self.request_url = "/api/event/dispose"
        url = self.web_url + self.request_url
        self.request_method = "POST"
        self.request_params = {"method": method, "id": event_id, "remark": remark}

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response

    def update_event_status_from_undispose_to_tobedispose(self):
        self.request_url = "/api/event/updateAllRealtimeEventStatus"
        url = self.web_url + self.request_url
        self.request_method = "GET"

        response = HTTPRequest().http_request(method=self.request_method, url=url, headers=self.request_headers,
                                              params=self.request_params, cookies=self.cookies)
        return response
